package cn.sise.edu.crm.util;


import cn.sise.edu.crm.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by chen on 2019/11/6.
 */
public abstract class UserContext {
    public static final  String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final  String PERMISSIONEXPRESSIONS_IN_SESSION = "PERMISSIONEXPRESSIONS_IN_SESSION";
    public static HttpServletRequest getRequest(){
     return  ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static HttpSession getSession() {
        HttpSession session = getRequest().getSession();
        return session;
    }
    //往session存入登录用户
    public static void setEmployee(Employee employee){
        HttpSession session = getSession();
        session.setAttribute(EMPLOYEE_IN_SESSION,employee);
    }
    //从session取出登录用户
    public static Employee getEmployee(){
        HttpSession session = getSession();
        return (Employee) session.getAttribute("EMPLOYEE_IN_SESSION");
    }
    //把查询权限信息存好
    public static void setExpressions(List<String> permissionExpressions) {
        getSession().setAttribute("PERMISSIONEXPRESSIONS_IN_SESSION",permissionExpressions);
    }
    //获取得到权限列表
    public static List<String> getExpressions() {
        return ((List<String>) getSession().getAttribute("PERMISSIONEXPRESSIONS_IN_SESSION"));
    }
}

